<?php defined('SYSPATH') or die('No direct script access.');

class Controller_User_Forgot extends Controller_DefaultTemplate{

public function action_index()
{
	$this->template->content = View::factory('/user/forgot');
}

public function action_submit()
{
	if ($_POST)
    {
		$email = arr::get($_POST, 'email');
		$user = ORM::factory('user');
		$user->where('email', '=', $email)->find();
		if ($user->loaded())
		{
			$this->send_forgot_email($user);
			$this->request->redirect('/user/forgot/forgotResult');
			return;
		}
	}
	$this->template->content = View::factory('/user/forgot');
}

public function action_forgotResult()
{
	$this->template->content = View::factory('/user/forgotResult');
}

public function action_reset()
{
	if ($_GET)
	{
		$check_code = arr::get($_GET, 'p');	
		$check_code_array = $this->get_check_code_array($check_code);

		$user = ORM::factory('user');
		$user->where('email', '=', $check_code_array['0'])->find();
		if ($user->loaded())
		{
			$find_check_code = md5($check_code_array['0'].'+'.$user->password);
			if( $check_code_array['1'] === $find_check_code )
			{
				$this->template->content = View::factory('/user/reset_password');
				$this->template->content->set("check_code", $check_code);
				$this->template->content->set("email", $user->email);
				return;
			}
		}
	}
	$this->request->redirect('');	
}

public function action_reset_password()
{
	if ($_POST)
    {
		$check_code = arr::get($_POST, 'authenticity_token');
		$check_code_array = $this->get_check_code_array($check_code);
		$password = arr::get($_POST, 'newPassword');
		$password_confirm = arr::get($_POST, 'reTypeNewPassword');

		$user = ORM::factory('user');
		$user->where('email', '=', $check_code_array['0'])->find();
		if ($user->loaded())
		{
			$find_check_code = md5($check_code_array['0'].'+'.$user->password);
			if( $check_code_array['1'] === $find_check_code )
			{
				$user->update_user(
					array(
						'password' => $password,
						'password_confirm' => $password_confirm,
					));

				$this->request->redirect('/user/login');
				return;
			}
		
		}
	}
	$this->request->redirect('');
}


protected function send_forgot_email($user)
{
	$send_string = $this->get_check_code($user);
	Mailer::instance()
		->to($user->email)
		->from(array('info@comefit.com' => 'The Team'))
		->subject('Welcome!')
		->html("你使用了本站提供的密码找回功能，如果你确认此密码找回功能是你启用的，请点击下面的链接，按流程进行密码重设。<br><br><br>
		<a href='".URL::base()."user/forgot/reset?p=".$send_string."'>确认密码找回</a>")
		->send();
}

protected function get_check_code($user)
{
	$md5_email_password = md5($user->email.'+'.$user->password);
	return base64_encode($user->email." &".$md5_email_password);
}

protected function get_check_code_array($check_code)
{
	return explode(' &',base64_decode($check_code));
}

public function action_result()
{
	$this->template->content = View::factory('/user/forgotResult');
}

/**
* Initialize properties before running the controller methods (actions),
* so they are available to our action.
*/
public function before()
{
	// Run anything that need ot run before this.
	parent::before();
}

}


